var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    uploadImgList:[],
    inputContent:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    var that = this;
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
    });
  },
  bindTextInput(e) {
    var ctx = this;
    console.log(e)
    ctx.setData({
      inputContent: e.detail.value
    })
  },
  handleAddImage: function () {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(res);
        that.upload(that, tempFilePaths);
      }
    })
  },

  upload: function (page, path) {
    let vm = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
        url: cf.config.pageDomain + '/mobile/common/imgupload?cusmallToken=' + cusmallToken,
        filePath: path[0],
        name: 'file',
        header: {"Content-Type": "multipart/form-data" },
        formData: {
          //和服务器约定的token, 一般也可以放在header中
          cusmallToken: cusmallToken,
        },
        success: function (res) {
          console.log(res);
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          let data = JSON.parse(res.data);
          let uploadImgList = vm.data.uploadImgList;
          uploadImgList.push(data.fileName);
          vm.setData({
            uploadImgList: uploadImgList
          });
        },
        fail: function (e) {
          console.log(e);
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },

  handleDelImg: function (e) {
    let vm = this;
    let idx = e.target.dataset.index;
    let imgList = vm.data.uploadImgList;
    imgList.splice(idx, 1);
    vm.setData({
      uploadImgList: imgList
    })
  },

  handleSubmit(){
    let ctx = this;
    let cnt = ctx.data.inputContent;
    if("" === cnt){
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请填写内容'
      })
      return;
    }

    let imgList = "";
    if (ctx.data.uploadImgList.length) {
      imgList = ctx.data.uploadImgList.join(",")
    }
    wx.showLoading({
      title: "加载中",
    });
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/friendscircle/addFriendsCircle',
      data: {
        cusmallToken: cusmallToken,
        content: cnt,
        img: imgList
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        if (data && 0 === data.ret) {
          wx.showToast({
            title: "发布成功",
            icon: 'success',
            success(){
              let pages = getCurrentPages();//当前页面
              let prevPage = pages[pages.length - 2];//上一页面
              prevPage.setData({//直接给上移页面赋值
                isPublish: "yes"
              });
              wx.navigateBack({
                delta: 1
              })
            },
            duration: 1000
          });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail() {

      },
      complete(){
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {

  }
}))